package com.gofunbuy;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.gofunbuy.R;
import com.gofunbuy.BaseGroup;
import com.gofunbuy.TabFavourableGroup;
import com.gofunbuy.TabHomeGroup;
import com.gofunbuy.TabMemberGroup;
import com.gofunbuy.TabOthersGroup;
import com.gofunbuy.app.TabItem;
import com.gofunbuy.app.book.TabBookActivity;
import com.gofunbuy.app.favourable.TabFavourableActivity;
import com.gofunbuy.app.home.HomeActivity;
import com.gofunbuy.app.home.TabAboutActivity;
import com.gofunbuy.app.member.TabMemberActivity;
import com.gofunbuy.app.others.TabOthersActivity;
import com.gofunbuy.app.store.TabStoreActivity;
import com.gofunbuy.util.GoFunBuyUtils;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface;

/**
 * <p>
 * 整个流程就像使用ListView自定BaseAdapter一样
 * </p>
 * 
 * <p>
 * 如果要自定义TabHostActivity的Theme，并且不想要头部阴影 一定要添加这个android:windowContentOverlay =
 * null
 * </p>
 * 
 * <p>
 * 如果想在别的项目里面使用TabHostActivity 可以项目的属性里面找到Android，然后在Library部分添加这个项目(Api) <a
 * href
 * ="http://www.cnblogs.com/qianxudetianxia/archive/2011/05/01/2030232.html">
 * 如何添加</a>
 * </p>
 * */
public class MainActivity extends BaseGroup implements GoFunBuyInterface,
		OnClickListener {

	List<TabItem> mItems;
	private LayoutInflater mLayoutInflater;

	/**
	 * 在初始化TabWidget前调用 和TabWidget有关的必须在这里初始化
	 */
	@Override
	protected void prepare() {
		TabItem home = new TabItem(null, // title
				R.drawable.tab_home, // icon
				R.drawable.example_tab_item_bg, // background
				new Intent(this, TabHomeGroup.class)); // intent

		TabItem book = new TabItem(null, R.drawable.tab_reserve,
				R.drawable.example_tab_item_bg, new Intent(this,
						TabBookActivity.class));

		TabItem member = new TabItem(null, R.drawable.tab_member,
				R.drawable.example_tab_item_bg, new Intent(this,
						TabMemberGroup.class));

		TabItem reserve = new TabItem(null, R.drawable.tab_store,
				R.drawable.example_tab_item_bg, new Intent(this,
						TabStoreGroup.class));

		TabItem store = new TabItem(null, R.drawable.tab_favourable,
				R.drawable.example_tab_item_bg, new Intent(this,
						TabFavourableGroup.class));

		TabItem more = new TabItem(null, R.drawable.tab_other,
				R.drawable.example_tab_item_bg, new Intent(this,
						TabOthersGroup.class));
		mItems = new ArrayList<TabItem>();
		mItems.add(home);
		mItems.add(book);
		mItems.add(member);
		mItems.add(reserve);
		mItems.add(store);
		mItems.add(more);
		// 设置分割线
		TabWidget tabWidget = getTabWidget();
		tabWidget.setDividerDrawable(R.drawable.tab_divider);
		mLayoutInflater = getLayoutInflater();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		switch (getIntent().getIntExtra(TAB_POSE, 0)) {
		case TAB_HOME:
			setCurrentTab(0);
			break;
		case TAB_BOOKING:
			setCurrentTab(1);
			break;
		case TAB_MEMBER:
			setCurrentTab(2);
			break;
		case TAB_SHOPING:
			setCurrentTab(3);
			break;
		case TAB_FAVORABLE:
			setCurrentTab(4);
			break;
		case TAB_OHTERS:
			setCurrentTab(5);
			break;
		// default:
		// setCurrentTab(0);
		}
	}

	/** tab的title，icon，边距设定等等 */
	@Override
	protected void setTabItemTextView(TextView textView, int position) {
		// textView.setPadding(3, 3, 3, 3);
		// textView.setText(mItems.get(position).getTitle());

		// textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems
		// .get(position).getIcon(), 0, 0);

	}

	@Override
	protected void setTabItemTextView(ImageView imageView, int position) {
		// TODO Auto-generated method stub
		// imageView.setPadding(3, 3, 3, 3);
		imageView.setBackgroundResource(mItems.get(position).getBg());
		imageView.setImageResource(mItems.get(position).getIcon());
	}

	/** tab唯一的id */
	@Override
	protected String getTabItemId(int position) {
		return mItems.get(position).getTitle(); // 我们使用title来作为id，你也可以自定
	}

	/** 点击tab时触发的事件 */
	@Override
	protected Intent getTabItemIntent(int position) {
		return mItems.get(position).getIntent();
	}

	@Override
	protected int getTabItemCount() {
		return mItems.size();
	}

	/** 自定义头部文件 */
	@Override
	protected View getTop() {
		return mLayoutInflater.inflate(R.layout.topbar_layout, null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
